package com.jaynux.app;

import java.util.*;

import android.app.*;
import android.content.*;
import android.database.*;
import android.database.sqlite.*;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;

import com.jaynux.app.provider.*;
import com.jaynux.app.provider.SaveDatabases.*;
public class SaveInformationActivity extends Activity {

	final int _ID = 0;
	final int TITLE = 1;
	final int PARTICIPATION = 2;
	final int WIN = 3;
	final int DISTANCE = 4;
	final int RACETIME = 5;

	private ListView mMyList;	
	private SQLiteDatabase mDb;	// SQLite DB에 접근하기위한 변수
	private Cursor mCursor;
	private Cursor mCursorCondition;
	private SaveDatabases mSaveDatabases;
	// 커스터 마이징 한 어뎁터이다.
	ContactListItemAdapter mAdapter;

	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.loadsave);


		mMyList = (ListView)findViewById(R.id.gamesavelistview);
		mSaveDatabases = new SaveDatabases(this) ;
		mDb = mSaveDatabases.getWritableDatabase();

		mCursor = mDb.rawQuery("select _id, title, participation, win, distance, racetime from notes", null);

		mAdapter = new ContactListItemAdapter(this, R.layout.race_list_item, mCursor);
		mMyList.setAdapter(mAdapter);
	}
	OnClickListener pOnClickListener = new OnClickListener() {

		public void onClick(View v) {
		}
	};

	//커서 어뎁터의 커스터 마이징 원하는 값을 출력 해주기 위해서 한다.
	private final class ContactListItemAdapter extends ResourceCursorAdapter {
		public ContactListItemAdapter(Context context, int layout, Cursor c) {
			super(context, layout, c);
		}

		@Override
		public void bindView(View view, Context context, Cursor cursor) {
			final ContactListItemCache cache = (ContactListItemCache) view.getTag();
			//캐쉬에서 꺼내는 작업
			TextView bitemId = cache.itemId;
			TextView bitemName = cache.itemName;
			TextView bitemWin = cache.itemWin;
			TextView bitemRaceTime = cache.itemRaceTime;
			bitemId.setText(cursor.getString(_ID));
			bitemName.setText(cursor.getString(TITLE));

			//승률을 계산한다.
			if ( cursor.getInt(PARTICIPATION) != 0 && cursor.getInt(WIN) !=0){
				float participation,win;
				participation = (float)cursor.getInt(PARTICIPATION);
				win = (float)cursor.getInt(WIN);
				bitemWin.setText(String.format("1등횟수/출전횟수(확률):%d/%d(%.1f%%)",(int)win,(int)participation,(win/participation)*100.0));
			} else {
				bitemWin.setText(String.format("1등횟수/출전횟수(확률):0/%d(0%%)",cursor.getInt(PARTICIPATION)));
			}
			//평균속도를 계산한다.
			if ( cursor.getFloat(DISTANCE) != 0 && cursor.getInt(RACETIME) !=0){
				bitemRaceTime.setText(String.format("평속:%.2fm/sec",cursor.getInt(DISTANCE)/cursor.getFloat(RACETIME)));
			}
		}
		@Override
		public View newView(Context context, Cursor cursor, ViewGroup parent) {
			View view = super.newView(context, cursor, parent);
			ContactListItemCache cache = new ContactListItemCache();
			cache.itemId = (TextView) view.findViewById(R.id.Raceinfor_number);
			cache.itemName = (TextView) view.findViewById(R.id.Raceinfor_name);
			cache.itemWin = (TextView) view.findViewById(R.id.Raceinfor_wining);
			cache.itemRaceTime = (TextView) view.findViewById(R.id.Raceinfor_average_speed);
			view.setTag(cache);
			return view;
		}
	}
	final static class ContactListItemCache {
		public TextView itemId;
		public TextView itemName;
		public TextView itemWin;
		public TextView itemRaceTime;
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		//DB를 쓰고난 다음에 닫아 주어야 한다.
		mDb.close();
	}
	
}
